<?php
// 引入公共文件
include_once('./config/config.php');
// 引入数据库配置文件
include_once('./config/check.php');

//检验用户名
$action = isset($_GET['action'])?$_GET['action']:'';

if ($action == 'add') {
  $result = [
    'code' => 0,
    'msg' => ''
  ];

  $name = isset($_POST['name'])?$_POST['name']:'';

  $sql = "select * from per_department where name='$name'";

  $position = find($sql);
  if ($position) {
    $result['code'] = 0;
    $result['msg'] = '职位已存在';
  }else {
    $result = [
      'code' => 1,
      'msg' => '可以注册'
    ];
  }
  echo json_encode($result);
  exit;
}

if ($_POST) {
  $name = isset($_POST['name'])?$_POST['name']:'';


  $sql = "SELECT * FROM per_department WHERE name='$name'";
  $result = find($sql);
  if ($result) {
    ShowMsg('职位已存在');
    exit;
  }

  
  $data = [
    'name' => $name
  ];
  $res = add('per_department', $data);
  if ($res) {
    ShowMsg('添加成功', 'depList.php');
    exit;
  }
  ShowMsg('添加失败');
  exit;
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
  <?php include_once('common/meta.php');?>
  </head>
  <body> 
    
    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">添加部门</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.html">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                    
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='depList.php'"><i class="icon-list"></i> 部门列表</button>
                  <div class="btn-group">
                  </div>
                </div>

                <div class="well">
                    <div id="myTabContent" class="tab-content">
                      <div class="tab-pane active in" id="home">
                        <form method="post" enctype="multipart/form-data">
                            <label>部门</label>
                            <input type="text" class="input-xlarge" name="name" id="name">
                            <span id="tips" style="display: none;"></span>
                            <label></label>
                            <label></label>
                            <input class="btn btn-primary" type="submit" value="提交" />
                        </form>
                      </div>
                  </div>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Delete Confirmation</h3>
                  </div>
                  <div class="modal-body">
                    
                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                  </div>
                  <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                  </div>
                </div>
                
                <?php include_once('common/footer.php'); ?>
                    
            </div>
        </div>
    </div>
  </body>
</html>
<?php include_once('common/script.php');?>

<script>
  $('input[name="name"]').change(function(){
    $.ajax({
      type: 'post',
      url: 'depAdd.php?action=add',
      dataType: 'json',
      data: {
        name: $(this).val()
      },
      success: function(res) {
        if (res.code == 0) {
          $("#tips").css({
            "color": "red",
            "display": "inline-block"
          })
          $("#tips").html(res.msg);
          $('input[type="submit]').attr('disabled',true)
        }else {
          $("#tips").css({
            "color": "green",
            "display": "inline-block"
          })
          $("#tips").html(res.msg);
          $('input[type="submit]').attr('disabled',false)
        }
      }
    })
  })
</script>

